package com.globallogic.vacationplanner.model.service.impl;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.globallogic.vacationplanner.model.dao.declaration.IUserDao;
import com.globallogic.vacationplanner.model.entity.User;
import com.globallogic.vacationplanner.model.helper.DtoHelper;
import com.globallogic.vacationplanner.transfer.dto.impl.UserDto;
import com.globallogic.vacationplanner.transfer.exception.DaoException;
import com.globallogic.vacationplanner.transfer.service.IUserService;

@Service("userService")
public class UserService implements IUserService {
	private static final long serialVersionUID = 3112903289101718048L;

	private static Logger LOGGER = Logger.getLogger(UserService.class);
	private IUserDao userDao;

	public UserService() {
		LOGGER.info("User service was created successfully");
	}

	@Autowired
	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	private boolean isLoginExist(String login) {
		LOGGER.info("find user by login");
		boolean returnValue = userDao.findUserByLogin(login);
		if (returnValue) {
			return returnValue;
		}
		return false;
	}

	private boolean isUserExist(String login, String password) {
		LOGGER.info("check is user exist");
		boolean contain = false;
		User user = new User();
		user.setLogin(login);
		user.setPassword(password);
		contain = userDao.read(user) != null;
		LOGGER.info("Is user exist: " + contain);
		return contain;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @throws DaoException
	 */
	@Override
	@Transactional
	public boolean register(UserDto userDto) throws DaoException {
		LOGGER.info("start register new user");
		if (isLoginExist(userDto.getLogin()) == false) {
			return false;
		} else {
			LOGGER.info("start configure new user");
			User user = new User(userDto.getLogin(), userDto.getPassword(),
					userDto.getUserName(), userDto.getSurname(),
					userDto.getEmail());
			userDao.insert(user);
			LOGGER.info("Insert  new user in database");
			LOGGER.info("register new user was finished success");
		}
		return true;
	}

	@Override
	public boolean authorize(UserDto userDto) {
		LOGGER.info("start authorize user");
		if (isUserExist(userDto.getLogin(), userDto.getPassword()) == false) {
			LOGGER.info("User exist, but: ");
			LOGGER.info("incorrect login or password");
			return false;
		} else {
			User user = new User();
			user.setLogin(userDto.getLogin());
			user = userDao.read(user);
			DtoHelper.convertUserToUserDto(user, userDto);
			LOGGER.info("Authorizing was finished success");
		}
		return true;
	}

	@Override
	public void changeProfile(UserDto userDto) throws DaoException {
		LOGGER.info("start change user profile");
		User user = DtoHelper.convertUserDtoToUser(userDto);
		user = userDao.read(user);
		user.setUsername(userDto.getUserName());
		user.setSurname(userDto.getSurname());
		user.setEmail(userDto.getEmail());
		user.setPassword(userDto.getPassword());
		userDao.update(user);
		LOGGER.info("profile was changed success");
	}
}
